<?php

namespace App\Admin\Forms\Customer;

use App\Models\Customer;
use Dcat\Admin\Contracts\LazyRenderable;
use Dcat\Admin\Http\JsonResponse;
use Dcat\Admin\Traits\LazyWidget;
use Dcat\Admin\Widgets\Form;
use Illuminate\Support\Facades\DB;

class RemarkForm extends Form implements LazyRenderable
{
    use LazyWidget;

    /**
     * Handle the form request.
     *
     * @param array $input
     * @return JsonResponse
     */
    public function handle(array $input): JsonResponse
    {

        DB::transaction(function () use ($input) {
            $customer = Customer::query()->where('id', $this->payload['id'])->first();
            if ($this->payload['is_sale']) {
                $customer->sale_remark = $input['remark'];
            } else {
                $customer->return_remark = $input['remark'];
            }
            $customer->save();
        }, 2);
        return $this
            ->response()
            ->success(trans('admin.processed_successfully'))
            ->refresh();
    }

    /**
     * Build a form here.
     */
    public function form()
    {
        $this->textarea('remark', '备注')->required()->default($this->payload['remark'] ?? '');
    }

}
